$(function () {
    let currentPage = 1
    //文章列表查询
    let newType = ''
    let newState = ''
    let perpage = 10
    $.ajax({
        url: BigNew.category_list,
        headers: { Authorization: localStorage.getItem('bignews_token') },
        dataType: 'json',
        success: function (res) {
            // console.log(res)
            $('#selCategory').html(template('categoryTemp', res))

        }
    })
    $('#btnSearch').on('click', function () {
        // console.log(1)
        // console.log($('#selCategory').val())
        // console.log($('#selStatus').val())
        newType = $('#selCategory').val()
        newState = $('#selStatus').val()
        // init($('#selCategory').val(), $('#selStatus').val())
        currentPage = 1
        init(newType, newState)
    })


    let sumPage
    let sumCount
    //文章列表的渲染
    function init(type, state) {
        $.ajax({
            url: BigNew.article_query,
            headers: { Authorization: localStorage.getItem('bignews_token') },
            data: { page: currentPage, perpage, type, state },
            dataType: "json",
            success: function (res) {
                // console.log(res)
                $('tbody').html(template('articleTemp', res.data))
                // console.log(res.data.totalPage)
                //总页数
                sumPage = res.data.totalPage
                //总文章数
                sumCount = res.data.totalCount
                // console.log(sumCount % 6)

                setPage(res.data.totalPage)

            }
        });
    }
    init()

    //设置分页
    function setPage(pageSum) {

        $(".pagination").bootstrapPaginator({
            //设置版本号
            bootstrapMajorVersion: 3,
            // 显示第几页
            currentPage: currentPage,
            // 总页数
            totalPages: pageSum || 1,
            //当单击操作按钮的时候, 执行该函数, 调用ajax渲染页面
            onPageClicked: function (event, originalEvent, type, page) {
                // 把当前点击的页码赋值给currentPage, 调用ajax,渲染页面
                currentPage = page
                init(newType, newState)
            }
        })

    }

    //删除
    $('tbody').on('click', '.delete', function () {
        // console.log(1)
        if (sumCount % perpage == 1) {
            if (currentPage > 1) {
                currentPage = sumPage - 1
            }
        }
        let id = $(this).data().id
        // console.log(id)
        if (confirm('是否删除此文章?')) {
            $.ajax({
                type: "post",
                url: BigNew.article_delete,
                headers: { Authorization: localStorage.getItem('bignews_token') },
                data: { id },
                dataType: "json",
                success: function (res) {
                    // console.log(res)
                    // init(newType, newState)
                    alert(res.msg)
                    init(newType, newState)
                }
            });
        }
    })



    //每页显示新闻数
    $('#showCount').on('change', function () {
        // console.log($(this).val())
        perpage = $(this).val()
        currentPage = 1
        init(newType, newState)
    })

    //点击发表文章,改变首页发表文章的样式
    // console.log(window.parent)
    $('#release_btn').on('click', function () {
        window.parent.$('.level02 li').eq(1).addClass('active').siblings().removeClass('active')
    })

    $('tbody').on('click', '.btnEdit', function () {
        window.parent.$('.level02 li').eq(1).addClass('active').siblings().removeClass('active')
    })

})